﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TYVA07 : UIVAART
    {
        #region ==TBMAT===========
        public static readonly DependencyProperty TBMATProperty = DependencyProperty.Register("TBMAT",
            typeof(DataTable), typeof(TYVA07),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OTBMATChanged));
        public DataTable TBMAT
        {
            get
            {
                return (DataTable)GetValue(TBMATProperty);
            }
            set
            {
                SetValue(TBMATProperty, value);
            }
        }
        private static void OTBMATChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TYVA07;
            if (src != null)
            {
                src.TBMAT = (DataTable)e.NewValue;
            }
        }
        #endregion
        public TYVA07()
        {

            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAART();
            this.DataContext = MV;
            InitializeComponent();
        }
       
        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
            if (ORDERNUM.Length < 29)
                return;
            if (!IsAutoVA)
                return; 
            if (TBMain.Rows.Count > 0)
                return; 
            dwData = TBMain.NewRow();
            ((VVAART)MV).SetUpNewRow(ref dwData);
            dwData["VAAMANDANT"] = app.Mandant;
            dwData["VAAKOLL"] = ORDERNUM.Substring(0, 5);
            dwData["VAAART"] = ORDERNUM.Substring(5, 24);
            dwData["VAADART"] = ORDERNUM.Substring(5, 20);
            dwData["VAADVAR"] = "  ";
            if (TBMAT != null)
            {
                int matpos = 1;
                double pecentage = 0;
                string c = "";
                string yc = "";
                foreach (DataRow row in TBMAT.Rows)
                {
                    string sv = row["GSMRANT"].ToString();
                    if (sv == "")
                        sv = "0";

                    c = row["GSMRFARBE"].ToString().Trim();
                     row["GSMRSPZ"] = "C";
                    if (c != "C")
                    {
                        yc += c;
                    }
                    double ratio = Convert.ToDouble(sv);
                    pecentage += ratio;
                    dwData["VAAGESANT" + matpos] = ratio;
                    dwData["VAAGESBEZ" + matpos] = row["GSMRSPZ"];
                    matpos++;
                }
            }
            ((VVAART)MV).Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
        }
     }
}
